﻿using System;
using System.Reflection;
using MessagePack.Formatters;
using MessagePack.Internal;

namespace MessagePack.Resolvers
{
	// Token: 0x0200010E RID: 270
	public class DynamicObjectResolver : IFormatterResolver
	{
		// Token: 0x060003CF RID: 975 RVA: 0x0000E71B File Offset: 0x0000CB1B
		private DynamicObjectResolver()
		{
		}

		// Token: 0x060003D1 RID: 977 RVA: 0x0000E73E File Offset: 0x0000CB3E
		public IMessagePackFormatter<T> GetFormatter<T>()
		{
			return DynamicObjectResolver.FormatterCache<T>.formatter;
		}

		// Token: 0x0400016A RID: 362
		public static readonly DynamicObjectResolver Instance = new DynamicObjectResolver();

		// Token: 0x0400016B RID: 363
		private const string ModuleName = "MessagePack.Resolvers.DynamicObjectResolver";

		// Token: 0x0400016C RID: 364
		internal static readonly DynamicAssembly assembly = new DynamicAssembly("MessagePack.Resolvers.DynamicObjectResolver");

		// Token: 0x0200010F RID: 271
		private static class FormatterCache<T>
		{
			// Token: 0x060003D2 RID: 978 RVA: 0x0000E748 File Offset: 0x0000CB48
			static FormatterCache()
			{
				TypeInfo_wzh typeInfo = typeof(T).GetTypeInfo_wzh();
				if (typeInfo.IsNullable())
				{
					typeInfo = typeInfo.GenericTypeArguments[0].GetTypeInfo_wzh();
					object formatterDynamic = DynamicObjectResolver.Instance.GetFormatterDynamic(typeInfo.AsType());
					if (formatterDynamic == null)
					{
						return;
					}
					DynamicObjectResolver.FormatterCache<T>.formatter = (IMessagePackFormatter<T>)Activator.CreateInstance(typeof(StaticNullableFormatter<>).MakeGenericType(new Type[]
					{
						typeInfo.AsType()
					}), new object[]
					{
						formatterDynamic
					});
					return;
				}
				else
				{
					TypeInfo_wzh typeInfo2 = DynamicObjectTypeBuilder.BuildType(DynamicObjectResolver.assembly, typeof(T), false);
					if (typeInfo2 == null)
					{
						return;
					}
					DynamicObjectResolver.FormatterCache<T>.formatter = (IMessagePackFormatter<T>)Activator.CreateInstance(typeInfo2.AsType());
					return;
				}
			}

			// Token: 0x0400016D RID: 365
			public static readonly IMessagePackFormatter<T> formatter;
		}
	}
}
